﻿package com.PGD.ui.presentation
{
	import com.PGD.events.SlidePackEvent;
	import com.PGD.model.constants.SlideType;
	import com.PGD.model.factories.SlideFactory;
	import com.PGD.model.vos.ISlide;
	import com.PGD.model.vos.ISlidePack;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import uranrus.persistence.BazManagedCollection;
	
	[Bindable]
	public class SlideAdminPresentationModel extends EventDispatcher
	{
		public var dispatcher:IEventDispatcher;
		
		
		public function SlideAdminPresentationModel(disp:IEventDispatcher)
		{
			dispatcher = disp;
			
		}
		
		private var _slidePack:ISlidePack;
		[Bindable(event="slidePackChange")]
		public function set slidePack (value:ISlidePack):void
		{
			
			if (value != _slidePack)
			{
				_slidePack = value;
				dispatchEvent (new Event ("slidePackChange"));
				dispatchEvent (new Event ("slidesChange"));
				
			}
			if(value)
			{
				slidePack.lastSaved = value.lastSaved;
				dispatchEvent (new Event ("slidePackChange"));
				
			}
		}

		public function get slidePack ():ISlidePack
		{
			return _slidePack;
		}

		/* private var _slides:ArrayCollection = new ArrayCollection;
		[Bindable(event="slidesChange")]
		public function set slides (value:ArrayCollection):void
		{
			if (value != _slides)
			{
				_slides = value;
				dispatchEvent (new Event ("slidesChange"));
			}
		}

		public function get slides ():ArrayCollection
		{
			return slidePack.slides;
		}
		 */
		

	



		private var _selectedSlide:ISlide;
		[Bindable(event="selectedSlideChange")]
		public function set selectedSlide (value:ISlide):void
		{
			if (value != _selectedSlide)
			{
				_selectedSlide = value;
				dispatchEvent (new Event ("selectedSlideChange"));
			}
		}

		public function get selectedSlide ():ISlide
		{
			return _selectedSlide;
		}

		private var _currentSlideIndex:int;
		[Bindable(event="currentSlideIndexChange")]
		public function set currentSlideIndex (value:int):void
		{
			if (value != _currentSlideIndex)
			{
				_currentSlideIndex = value;
				dispatchEvent (new Event ("currentSlideIndexChange"));
			}
		}

		public function get currentSlideIndex ():int
		{
			return _currentSlideIndex;
		}


		
		
			
			
		
		
		


		
		public function addSlide(type:String):void
		{
			try
			{
			var slide:ISlide
			var slides:BazManagedCollection = slidePack.slides as BazManagedCollection
			if(slides)
			{
				var position:int;
				var index:int;
				if(selectedSlide)
				{
					position = selectedSlide.timecode+1;
					index = selectedSlide.timecode;
				}
				else
				{
					position = slides.length;
					index = slides.length;
				}
				switch(type)
				{
					case SlideType.TEXT_SLIDE:
					slide = new SlideFactory().makeNewTextSlide(position, slidePack.id);
					slides.addItemAtIndex(slide,index);
					break;
					case SlideType.MULTIPLE_CHOICE_QUESTION_SLIDE:
					slide = new SlideFactory().makeNewMultipleChoiceQuestionSlide(position, slidePack.id);
					slides.addItemAtIndex(slide,index);
					break;
				}
				dispatchEvent (new Event ("slidesChange"));
			}
			}
			catch(err:Error)
			{
				trace("adding slide error "+err);
			}
		}
		
		
		public function deleteSlide(slide:ISlide):void
		{
			try
			{
			var slides:BazManagedCollection = slidePack.slides as BazManagedCollection;
			slides.remove(slide);
			}
			catch(err:Error)
			{
				trace("deleting slide error "+err);
			}
				
			
		}
			
	

		
		public function saveSlidePack():void
		{
			if(slidePack != null)
			{
				var evt:SlidePackEvent = new SlidePackEvent(SlidePackEvent.UPDATE_DB);
				dispatcher.dispatchEvent(evt);
			}
		}
		
		
		
		
		public function setSelectedSlideProperty(propertyName:String, value:String):void
		{
			if(selectedSlide && slidePack.slides)
			{
			var slides:BazManagedCollection = slidePack.slides as BazManagedCollection;
			slides.updateItemProperty(selectedSlide,propertyName,value);
			}
		}
		
		

	}
}